<?php
/**
 * 
 * @author libai.tc <libai.tc@gmail.com>
 */

class productModel
{
        private $_db;

        public function __construct() {
                $this->_db = new DB();
        }

        /*从数据库中获取产品
         *参数说明:$ca 产品的分类id
         *$start   开始的行号
         *$step    取多少行数据
         */
        public function getProduct($ca, $start, $step=28){
                $db=$this->_db;
                if($start<=1) 
                        $start=0;
                else
                        $start=($start-2)*$step+8;
                $category=intval($ca);
                if($category<=0){
                        $sql="select * from product order by click_counts desc limit $start,$step";
                        $result=$db->get_all($sql);
                }
                else {
                        $id_set=array($category);
                        $child_set=$db->get_all("select id from category where father=$category");
                        foreach($child_set as $value)
                                array_push($id_set, $value['id']);
                        $id_str=implode(',', $id_set);
                        $sql="select * from product where category in ($id_str) order by click_counts desc limit $start,$step";
                        $result=$db->get_all($sql);
                }
                if(empty($result))
                        return false;
                else
                        return $result;
        }
        
        public function updateClickCounts(){
            $id=Base::getRequest('id');
            if($this->_db->query("update `product` set `click_counts`=`click_counts`+1 where id=$id")) return array('success'=>false);
            return array('success'=>true);
        }
}
/* ------ EOF ------ */

